package com.wbp.cms.core.customer.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.google.common.base.Preconditions;
import com.wbp.cms.client.customer.domain.CustomerDO;
import com.wbp.cms.client.customer.query.CustomerQuery;
import com.wbp.cms.client.customer.service.CustomerService;
import com.wbp.cms.client.result.BaseResultCode;
import com.wbp.cms.client.result.Result;
import com.wbp.cms.client.user.domain.UserDO;
import com.wbp.cms.core.customer.dao.CustomerDAO;
import com.wbp.cms.core.exception.ServiceException;
import com.wbp.cms.core.user.dao.UserDAO;


/**
 * @Description:
 * @Department:
 * @author andy.lhc
 * @date 2016年3月9日 下午2:20:24
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

  private Logger logger = Logger.getLogger(CustomerServiceImpl.class);
  /**
   * 
   */
  private static final long serialVersionUID = -135382499120933134L;

  @Resource(name = "customerDAO")
  private CustomerDAO customerDAO;
  
  @Resource(name="userDAO") 
  private UserDAO  userDAO ;

  @Override
  public Long addCustomer(CustomerDO customerDO) {
    
    return customerDAO.insertCustomer(customerDO);
  }

  @Override
  public boolean modifyCustomer(CustomerDO customerDO) {
    return customerDAO.updateCustomer(customerDO) > 0 ;
  }


  @Override
  public CustomerDO queryCustomerById(Long customerId) {
    if(customerId == null || customerId<=0)
      return null ;
    return  customerDAO.queryCustomerById(customerId) ;
  }

  public CustomerDO queryCustomerByUserName(String userName){
        Preconditions.checkArgument(StringUtils.isNotEmpty(userName), "userName不能为空");
        CustomerQuery  customerQuery = new CustomerQuery(); 
        customerQuery.setUserName(userName); 
        Result<List<CustomerDO>> cr =  this.queryUsers(customerQuery) ;
        if(cr.isSuccess() && cr.getData() !=null && !cr.getData().isEmpty() )
              return cr.getData().get(0) ;
        return null ;
  }
  @Override
  public Result<List<CustomerDO>> queryUsers(CustomerQuery customerQuery) {
    if (customerQuery == null)
      return Result.buildError(BaseResultCode.INVALID_PARAMETER);
    int count = 0;
    List<CustomerDO> customerList = null;
    try {

      if (customerQuery.getRecommendUserOrganizationId() == null
          || customerQuery.getRecommendUserOrganizationId() <= 0) {
        count = customerDAO.queryCustomerCount(customerQuery);
        if (count > 0)
          customerList = customerDAO.queryCustomer(customerQuery);
      } else {
        count = customerDAO.queryCustomerByUserCount(customerQuery);
        if (count > 0)
          customerList = customerDAO.queryCustomerByUser(customerQuery);
      }
      
      if(customerList !=null && customerList !=null){
          for(CustomerDO cd : customerList){
               UserDO  user = this.userDAO.load( cd.getUserId() ) ;
               if(user !=null && user.getUserName() !=null)
                   cd.setRecommendUserName(user.getUserName());
          }
      }

    } catch (Exception ex) {
      logger.error(String.format("failed to queryUsers customerQuery:", customerQuery), ex);
      //  return Result.buildError(ex.getMessage());
      throw new ServiceException(BaseResultCode.DB_ERROR.getErrMsg(), ex) ;
    }

    return Result.buildSuccess(count, customerList);
  }


}
